package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.api.domain.RemoteResourceTypeService;
import cn.cdeden.knowledge.api.domain.RemoteSchedulePermissionsService;
import cn.cdeden.knowledge.api.domain.vo.RemoteResourceTypeVo;
import cn.cdeden.knowledge.api.domain.vo.RemoteScheduleDetailsVo;
import cn.cdeden.knowledge.api.domain.vo.RemoteSchedulePermissionsVo;
import cn.cdeden.knowledge.domain.SysPermissionsRelevance;
import cn.cdeden.knowledge.domain.SysResourceType;
import cn.cdeden.knowledge.domain.SysScheduleDetails;
import cn.cdeden.knowledge.domain.SysSchedulePermissions;
import cn.cdeden.knowledge.domain.dto.SysSchedulePermissionsDto;
import cn.cdeden.knowledge.service.SysPermissionsRelevanceService;
import cn.cdeden.knowledge.service.SysResourceTypeService;
import cn.cdeden.knowledge.service.SysScheduleDetailsService;
import cn.cdeden.knowledge.service.SysSchedulePermissionsService;
import cn.cdeden.knowledge.utils.BeanCopyUtil;
import cn.cdeden.system.api.model.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@DubboService
public class RemoteSchedulePermissionsServiceImpl implements RemoteSchedulePermissionsService {

    @Autowired
    private SysSchedulePermissionsService sysSchedulePermissionsService;

    @Autowired
    private SysPermissionsRelevanceService sysPermissionsRelevanceService;

    @Autowired
    private SysScheduleDetailsService sysScheduleDetailsService;

    @Override
    public List<RemoteSchedulePermissionsVo> getAll() {
        return MapstructUtils.convert(sysSchedulePermissionsService.getAllPermissions(),RemoteSchedulePermissionsVo.class);
    }

    @Override
    public boolean add(RemoteSchedulePermissionsVo remoteSchedulePermissionsVo) {
        SysSchedulePermissionsDto sysSchedulePermissionsDto = MapstructUtils.convert(remoteSchedulePermissionsVo, SysSchedulePermissionsDto.class);

        SysSchedulePermissions sysSchedulePermissions = new SysSchedulePermissions();
        BeanCopyUtil.copyProperties(sysSchedulePermissionsDto, sysSchedulePermissions);
        LoginUser user = LoginHelper.getLoginUser();
        sysSchedulePermissions.setCreateBy(user.getUserId());
        List<SysSchedulePermissions> sysSchedulePermissionsList = sysSchedulePermissionsService.list(new QueryWrapper<SysSchedulePermissions>().eq("permissions_name", sysSchedulePermissionsDto.getPermissionsName()));
        if (!sysSchedulePermissionsList.isEmpty()) {
            throw new RuntimeException("该权限已存在！");
        }
        sysSchedulePermissionsService.save(sysSchedulePermissions);
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = new ArrayList<>();
        sysSchedulePermissionsDto.getDetailsIds().forEach(v -> {
            SysPermissionsRelevance sysPermissionsRelevance = new SysPermissionsRelevance();
            sysPermissionsRelevance.setPermissionsId(sysSchedulePermissions.getPermissionsId());
            sysPermissionsRelevance.setDetailsId(v);
            sysPermissionsRelevanceList.add(sysPermissionsRelevance);
        });
        return sysPermissionsRelevanceService.saveBatch(sysPermissionsRelevanceList);
    }

    @Override
    public RemoteSchedulePermissionsVo editQuery(Long id) {
        SysSchedulePermissionsDto sysSchedulePermissionsDto = new SysSchedulePermissionsDto();
        List<SysScheduleDetails> sysScheduleDetailsList = new ArrayList<>();
        SysSchedulePermissions sysSchedulePermissions = sysSchedulePermissionsService.getById(id);
        BeanCopyUtil.copyProperties(sysSchedulePermissions, sysSchedulePermissionsDto);
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = sysPermissionsRelevanceService.list(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", id));
        List<Long> detailsIdList = sysPermissionsRelevanceList.stream().map(SysPermissionsRelevance::getDetailsId).collect(Collectors.toList());

        if (!detailsIdList.isEmpty()) {
            sysScheduleDetailsList = sysScheduleDetailsService.list(new QueryWrapper<SysScheduleDetails>().select("details_id", "details_name", "`show`").in("details_id", detailsIdList));
        }
        sysSchedulePermissionsDto.setSysScheduleDetailsList(sysScheduleDetailsList);
        LoginUser loginUser = LoginHelper.getLoginUserByUserId(sysSchedulePermissions.getCreateBy());
        sysSchedulePermissionsDto.setCreateUserName(loginUser.getNickname());
        return MapstructUtils.convert(sysSchedulePermissionsDto,RemoteSchedulePermissionsVo.class);
    }

    @Override
    public boolean edit(RemoteSchedulePermissionsVo remoteSchedulePermissionsVo) {
        SysSchedulePermissionsDto sysSchedulePermissionsDto = MapstructUtils.convert(remoteSchedulePermissionsVo, SysSchedulePermissionsDto.class);

        SysSchedulePermissions sysSchedulePermissions = new SysSchedulePermissions();
        BeanCopyUtil.copyProperties(sysSchedulePermissionsDto, sysSchedulePermissions);
        List<SysSchedulePermissions> sysSchedulePermissionsList = sysSchedulePermissionsService.list(new QueryWrapper<SysSchedulePermissions>()
            .eq("permissions_name", sysSchedulePermissionsDto.getPermissionsName()).ne("permissions_id", sysSchedulePermissionsDto.getPermissionsId()));
        if (!sysSchedulePermissionsList.isEmpty()) {
            throw new RuntimeException("该权限已存在！");
        }
        boolean b = sysSchedulePermissionsService.updateById(sysSchedulePermissions);
        sysPermissionsRelevanceService.remove(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", sysSchedulePermissions.getPermissionsId()));
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = new ArrayList<>();
        sysSchedulePermissionsDto.getDetailsIds().forEach(v -> {
            SysPermissionsRelevance sysPermissionsRelevance = new SysPermissionsRelevance();
            sysPermissionsRelevance.setPermissionsId(sysSchedulePermissionsDto.getPermissionsId());
            sysPermissionsRelevance.setDetailsId(v);
            sysPermissionsRelevanceList.add(sysPermissionsRelevance);
        });
        sysPermissionsRelevanceService.saveBatch(sysPermissionsRelevanceList);
        return false;
    }

    @Override
    public boolean delete(Long id) {
        sysSchedulePermissionsService.removeById(id);
        return sysPermissionsRelevanceService.remove(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", id));
    }

    @Override
    public List<RemoteScheduleDetailsVo> getRelevance() {
        List<SysScheduleDetails> list = sysScheduleDetailsService.list(new QueryWrapper<SysScheduleDetails>().select("details_id", "details_name", "`show`").eq("`show`", 1));
        return MapstructUtils.convert(list,RemoteScheduleDetailsVo.class);
    }
}
